package com.mengxuegu.security.authrntication.code;

import com.mengxuegu.security.authrntication.CustomAuthenticationFailureHandler;
import com.mengxuegu.security.authrntication.exception.ValidateCodeException;
import com.mengxuegu.security.controller.CustomLoginController;
import com.mengxuegu.security.properites.SecurityProperties;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * OncePerRequestFilter   :所有请求之前被调用一次
 *
 * @author william
 * @create 2021-07-04 20:00
 **/
@Component("imageCodeValidateFilter")
public class ImageCodeValidateFilter extends OncePerRequestFilter {
    @Autowired
    private SecurityProperties securityProperties;
    @Autowired
    private CustomAuthenticationFailureHandler customAuthenticationFailureHandler;

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //1. 如果是登录请求,则校验输入的验证码是否正确
        if (securityProperties.getAuthentication().getLoginProcessingUrl()
                .equals(request.getRequestURI())
                && "post".equalsIgnoreCase(request.getMethod())) {
            try {
                //校验验证码合法性
                validate(request);
            }catch (AuthenticationException e){
                //交给自定义失败处理器进行处理异常
                customAuthenticationFailureHandler.onAuthenticationFailure(request,response,e);
                //一定要记得结束
                return;
            }
        }

        //放行请求
        filterChain.doFilter(request,response);


    }

    private void validate(HttpServletRequest request) {
        //先获取session中的验证码
        String sessionCode = (String) request.getSession().getAttribute(CustomLoginController.SESSION_KEY);
        //获取用户输入的验证码
        String inputCode = request.getParameter("code");
        if (StringUtils.isBlank(inputCode)) {
            throw new ValidateCodeException("验证码不能为空");
        }
        if (!inputCode.equalsIgnoreCase(sessionCode)) {
            throw new ValidateCodeException("验证码输入错误");
        }
    }
}
